package cn.edu.scnu.match.controller;

import cn.edu.scnu.annotation.IgnoreToken;
import cn.edu.scnu.match.service.MatchService;
import cn.edu.scnu.pojo.MatchActivity;
import cn.edu.scnu.utils.MapperUtil;
import cn.edu.scnu.utils.PrefixKey;
import cn.edu.scnu.vo.ResultCode;
import cn.edu.scnu.vo.SysResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import redis.clients.jedis.JedisCluster;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api
@RestController
public class MatchController {
    @Autowired
    private MatchService matchService;
    @Autowired
    private ObjectMapper mapper = MapperUtil.MP;
    @Autowired
    private JedisCluster jedis;
    @Lazy
    @Resource(type = RestTemplate.class)
    private RestTemplate restTemplate;

    /**
     * 获取所有的赛事活动
     *
     * @return 赛事活动列表
     */
    @IgnoreToken
    @ApiOperation("获取所有赛事活动")
    @GetMapping("/matchs")
    public SysResult getAllMatchs() {
        try {
            List<MatchActivity> matchs = matchService.getAllMatchs();
            return SysResult.build(ResultCode.SUCCESS, "获取所有赛事信息成功", matchs);
        } catch (Exception e) {
            return SysResult.build(ResultCode.FAIL, e.getMessage(), null);
        }
    }

    /**
     * 通过赛事编号获取赛事活动详细信息
     *
     * @param id 赛事编号
     * @return 赛事活动详细信息
     */
    @IgnoreToken
    @ApiOperation("通过赛事id获取赛事信息")
    @ApiImplicitParam(name = "id", value = "赛事编号", dataType = "String", paramType = "path", required = true)
    @GetMapping("/matchs/{id}")
    public SysResult getMatchById(@PathVariable("id") String id) {
        try {
            MatchActivity match = matchService.getMatchById(id);
            return SysResult.build(ResultCode.SUCCESS, "获取赛事信息成功", match);
        } catch (Exception e) {
            return SysResult.build(ResultCode.FAIL, e.getMessage(), null);
        }
    }

    /**
     * 通过用户名获取该用户所报名的所有赛事的信息
     *
     * @param userId 用户编号
     * @return 用户报名所有赛事的列表
     */
    @ApiOperation("通过用户编号获取该用户所报名的所有赛事的信息")
    @ApiImplicitParam(name = "userId", value = "用户编号", dataType = "String", paramType = "path", required = true)
    @GetMapping("/matchs/user/{userId}")
    public SysResult getMatchsByUsername(@PathVariable("userId") String userId) {
        try {
            List<MatchActivity> matchs = matchService.getMatchsByUserId(userId);
            return SysResult.build(ResultCode.SUCCESS, "获取报名赛事信息成功", matchs);
        } catch (Exception e) {
            return SysResult.build(ResultCode.FAIL, e.getMessage(), null);
        }
    }

    /**
     * 通过组织用户编号获取该组织用户创建的所有赛事的信息
     *
     * @param orgId 组织用户编号
     * @return 用户创建的所有赛事的列表
     */
    @IgnoreToken
    @ApiOperation("通过组织用户编号获取该组织用户创建的所有赛事的信息")
    @ApiImplicitParam(name = "orgId", value = "组织用户编号", dataType = "String", paramType = "path", required = true)
    @GetMapping("/matchs/org/{orgId}")
    public SysResult getMatchsByOrgId(@PathVariable("orgId") String orgId) {
        try {
            List<MatchActivity> matchs = matchService.getMatchsByOrgId(orgId);
            return SysResult.build(ResultCode.SUCCESS, "获取创建所有赛事成功", matchs);
        } catch (Exception e) {
            return SysResult.build(ResultCode.FAIL, e.getMessage(), null);
        }
    }

    /**
     * 创建赛事活动
     *
     * @param matchActivity 赛事活动信息
     * @return 操作响应码
     */
    @ApiOperation("创建赛事活动")
    @ApiImplicitParam(name = "matchActivity", value = "赛事详细信息", dataType = "cn.edu.scnu.pojo.MatchActivity", paramType = "body", required = true)
    @PostMapping("/matchs")
    public SysResult createMatch(@RequestBody MatchActivity matchActivity, HttpServletRequest httpServletRequest) {
        try {
            matchService.createMatch(matchActivity, httpServletRequest);
            return SysResult.build(ResultCode.CREATED, "创建赛事活动成功", null);
        } catch (Exception e) {
            return SysResult.build(ResultCode.FAIL, e.getMessage(), null);
        }
    }

    /**
     * 修改赛事信息
     *
     * @param matchActivity 赛事信息表
     * @return 操作响应码
     */
    @ApiOperation("修改赛事信息")
    @ApiImplicitParam(name = "matchActivity", value = "赛事详细信息", dataType = "cn.edu.scnu.pojo.MatchActivity", paramType = "body", required = true)
    @PutMapping("/matchs")
    public SysResult saveMatch(@RequestBody MatchActivity matchActivity) {
        try {
            matchService.saveMatch(matchActivity);
            return SysResult.build(ResultCode.SUCCESS, "修改赛事信息成功", null);
        } catch (Exception e) {
            return SysResult.build(ResultCode.FAIL, e.getMessage(), null);
        }
    }

    /**
     * 删除赛事
     *
     * @param id 赛事编号
     * @return 操作响应码
     */
    @ApiOperation("删除赛事活动")
    @ApiImplicitParam(name = "id", value = "赛事编号", dataType = "String", paramType = "path", required = true)
    @DeleteMapping("matchs/{id}")
    public SysResult deleteMatch(@PathVariable("id") String id, HttpServletRequest httpServletRequest) {
        try {
            matchService.deleteMatch(id, httpServletRequest);
            return SysResult.build(ResultCode.NO_CONTENT, "删除赛事成功", null);
        } catch (Exception e) {
            return SysResult.build(ResultCode.FAIL, e.getMessage(), null);
        }
    }
}
